<?php

namespace app\np\controller;
use app\base\controller\Base;
use think\Request;
use think\Cache;
use think\Config;
use think\Session;


class Department extends Base
{
    public  $model;

    public function __construct()
    {
        $this->model = model("Department");
        parent::__construct();
        $this->aliasPost = ["id"=>"id","name"=>"depart_name"];
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {//

        // user pass => sid =>reids
        // Session['sid'] =
        Cache::set("ak","123",3600);//
        echo  Cache::get("ak");exit;
     $res = $this->model->pages();
     return   resJson(1,$this->ecLang("query"),$res['res'],$res['pageInfo']);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $name = $this->$this->aliasPost['name'];
       $insert_id = $this->model->save([$name=>$this->$name]);
       if(empty($insert_id)){
           return   failJson($this->ecLang("failInsert"));
       }
        return   sucessJson($this->ecLang("scuessInsert"));
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
      $res =   $this->model->where(['id'=>$id])->find();
      return   sucessJson($this->ecLang("query"),$res);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
        $data = [$this->aliasPost['name']=>$this->$this->aliasPost['name'] ];
       $res = $this->model->save($data,['id' => $id]);
        if(empty($res))
        {
            return failJson($this->ecLang("failupdate"));
        }
        return   sucessJson($this->ecLang("scuessupdate"));
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
       $res =  $this->model->delete(['id'=>$id]);
        if(empty($res))
        {
            return failJson($this->ecLang("faildelete"));
        }
        return   sucessJson($this->ecLang("scuessdelete"));
    }



}
